package com.tjtt.tdm.util;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Node;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;

public class ProjectHelp {
	private  String filepath;
	private ActiveXComponent dotnetCom = null;
	public Dispatch pro = null;

	public ProjectHelp(String filepath)
	{
		dotnetCom = new ActiveXComponent("Tools.OfficeTool.ProjectTool");
		Dispatch.call(dotnetCom,"InitProject",filepath).toString(); 
	}
	
	public List getProjectList()
	{
		List<TaskEntity> entityList = new ArrayList<TaskEntity>();
		String projects = Dispatch.call(dotnetCom,"ReadProject").toString();
		System.out.print(projects);
		Document document;
		try {
			document = DocumentHelper.parseText(projects);
		    List lists = new ArrayList();
		    List  xmlNodeList = document.selectNodes("//project/task");
		    for(Iterator iter =  xmlNodeList.iterator();iter.hasNext();)
		    {
		    	Node xmlNode = (Node) iter.next();
		    	TaskEntity entity = new TaskEntity();
		    	entity.setName(xmlNode.valueOf("@Name"));
		    	entity.setFinish(xmlNode.valueOf("@Finish"));
		    	entity.setNotes(xmlNode.valueOf("@Notes"));
		    	entity.setOrder(xmlNode.valueOf("@Order"));
		    	entity.setOutlineLevel(xmlNode.valueOf("@OutlineLevel"));
		    	entity.setResourceNames(xmlNode.valueOf("@ResourceNames"));
		    	entity.setStart(xmlNode.valueOf("@Start"));
		    	entity.setUniqueID(xmlNode.valueOf("@UniqueID"));
		    	lists.add(entity);
		    }
		    return lists;
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
	
	public void SaveNoFile()
	{
		Dispatch.call(dotnetCom,"SaveNoFile").toString(); 
	}

	public String getFilepath() {
		return filepath;
	}

	public void setFilepath(String filepath) {
		this.filepath = filepath;
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		//d://test.mppF:\Tomcat 6.0\webapps\shtdm\\project\mppfile\/test.mpp
		//ProjectHelp ph = new ProjectHelp("d://test.mpp");
		ProjectHelp ph = new ProjectHelp("F:\\Tomcat 6.0\\webapps\\shtdm\\project\\mppfile\\/test.mpp");
		List<TaskEntity> entity = ph.getProjectList();
	}

}
